package week8.homework;
//************************************************************************************
//  CollectionDVD1.java    Author:Duanhzhixuan
//
//  Represents a collection of DVD movies.
//************************************************************************************
import week6.example.DVD;

import java.text.NumberFormat;
import java.util.Collection;
public class DVDCollection implements Comparable<DVDCollection>
{
 private DVD[] collection;
private int count;
private double totalCost;

    private String title, director;
    private int year;
    private double cost;
    private boolean bluray;//这是在Collection中重新声明变量

    //------------------------------------------------------------------------------------
//  Constructor: Creates an intially empty collection.
//------------------------------------------------------------------------------------
public DVDCollection(String title, String director, int year, double cost, boolean bluray)
{
   collection = new DVD[100];
  count = 0;
totalCost = 0.0;
    this.title = title ;
    this.director = director ;
    this.year = year;
    this.cost = cost;
    this.bluray = bluray ;//这是相当于再重新做了一个构造方法
}
//-------------------------------------------------------------------------------------
//  Adds a DVD to the collection, increasing the size of the collection array if 
// neccessary .
//------------------------------------------------------------------------------------
public void addDVD(String title, String director, int year, double cost, boolean bluray)
{
if (count == collection.length)
increaseSize();

collection[count] = new DVD(title, director, year, cost, bluray);
   totalCost += cost;
   count++;
}

//----------------------------------------------------------------------------------------------------
//Returns a report describing the DVD collection.
//----------------------------------------------------------------------------------------------------
public String toString()
{


    NumberFormat fmt = NumberFormat.getCurrencyInstance();

    String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    report += "My DVD Collection\n\n";

    report += "Number of DVDs:" + count+"\n";
    report += "Total cost:"+fmt.format(totalCost)+"\n";
    report += "Average cost:" + fmt.format(totalCost/count);

    report += "\n\nDVD List:\n\n";

    for (int dvd =0; dvd <count; dvd++)
        report += collection[dvd].toString()+ "\n";

    return report;
}

//-------------------------------------------------------------------------------------------------------
//  Increase the capacity of the collection by creating a larger array and copying the existing collection into it.
//-------------------------------------------------------------------------------------------------------
private void increaseSize() {
    DVD[] temp = new DVD[collection.length * 2];

    for (int dvd = 0; dvd < collection.length; dvd++)
        temp[dvd] = collection[dvd];

    collection = temp;
}


    public int compareTo(DVDCollection an) {
        int result;
        String titlea=an.getName();
        result=title.compareTo(titlea);
        return result;
    }//这是实现接口中的ComepareTo的方法
    public String getName()
    {
        return title;
    }//这是定义了一个get方法，返回title值。
}
